package ui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


import controller.MobilePlanSellAndBillSystem;
import customexceptions.ItemAlreadyExistsInPlanException;
import customexceptions.PlanAlreadyExistsException;
import customexceptions.PlanNotExistsException;
import customexceptions.ServiceAlreadyExistsException;
import customexceptions.ServiceNotExistsException;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class AddItemToPlanUI extends javax.swing.JFrame {
	private JTextField jTextFieldPlanNumber;
	private JButton jButtonAccept;
	private JTextField jTextFieldServiceCost;
	private JLabel jLabel1;
	private JLabel jLabelPlanNumber;
	private JTextField jTextFieldServiceName;
	private JLabel jLabelServiceName;
	private JTextField jTextFieldServiceFreeUnits;
	private JLabel jLabelServiceFreeUnits;
	private MobilePlanSellAndBillSystem system;
	private int planType;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AddItemToPlanUI inst = new AddItemToPlanUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	public AddItemToPlanUI(){
		
	}
	public AddItemToPlanUI(MobilePlanSellAndBillSystem system) {
		super();
		//initGUI();
		this.system = system;
	}
	
	public  void AddItemToTitularPlanUI(){
		planType = 0;
		initGUI();
	}
	public  void AddItemToAdditionalPlanUI(){
		planType = 1;
		initGUI();
	}

	public  void AddItemToPrePaidPlanUI(){
		planType = 2;
		initGUI();
	}
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jTextFieldPlanNumber = new JTextField();
				getContentPane().add(jTextFieldPlanNumber);
				jTextFieldPlanNumber.setBounds(138, 47, 333, 28);
			}
			{
				jLabelPlanNumber = new JLabel();
				getContentPane().add(jLabelPlanNumber);
				jLabelPlanNumber.setText("Plan Number");
				jLabelPlanNumber.setBounds(12, 59, 99, 16);
			}
			{
				jTextFieldServiceName = new JTextField();
				getContentPane().add(jTextFieldServiceName);
				jTextFieldServiceName.setBounds(138, 87, 333, 28);
			}
			{
				jLabelServiceName = new JLabel();
				getContentPane().add(jLabelServiceName);
				jLabelServiceName.setText("Service Name");
				jLabelServiceName.setBounds(12, 99, 114, 16);
			}		
			{
				jTextFieldServiceFreeUnits = new JTextField();
				getContentPane().add(jTextFieldServiceFreeUnits);
				jTextFieldServiceFreeUnits.setBounds(138, 139, 333, 28);
			}

			{
				jLabelServiceFreeUnits = new JLabel();
				getContentPane().add(jLabelServiceFreeUnits);
				jLabelServiceFreeUnits.setText("Service Free Units");
				jLabelServiceFreeUnits.setBounds(12, 151, 119, 16);
			}				
			{
				jButtonAccept = new JButton();
				getContentPane().add(jButtonAccept);
				jButtonAccept.setText("Create");
				jButtonAccept.setBounds(203, 242, 84, 23);

				jButtonAccept.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent evt) 
					{
						int  planNumber = Integer.parseInt(jTextFieldPlanNumber.getText());
						String serviceName = jTextFieldServiceName.getText();
						float serviceUnitCost = Float.parseFloat(jTextFieldServiceFreeUnits.getText());
						int serviceFreeUnits = Integer.parseInt(jTextFieldServiceFreeUnits.getText());

									try {
									if (planType == 0) {
										system.addItemToTitularPlan(planNumber,
													serviceName, serviceUnitCost, serviceFreeUnits);

									}else if (planType == 1) {
										system.addItemToAditionalPlan(planNumber,
												serviceName, serviceUnitCost, serviceFreeUnits);
									} else if (planType == 2) {
										system.addItemToPrePaidPlan(planNumber, 
												serviceName, serviceUnitCost, serviceFreeUnits);
									}

									} catch (PlanNotExistsException e) {
										ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
										errorReportView.setVisible(true);
									} catch (ServiceNotExistsException e){
										ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
										errorReportView.setVisible(true);			
									} catch ( ServiceAlreadyExistsException e){
										ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
										errorReportView.setVisible(true);
									}
							jTextFieldPlanNumber.setText("");
							jTextFieldServiceFreeUnits.setText("");

					}
				});
				
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Service Cost in Plan");
				jLabel1.setBounds(12, 191, 119, 16);
			}
			{
				jTextFieldServiceCost = new JTextField();
				getContentPane().add(jTextFieldServiceCost);
				jTextFieldServiceCost.setBounds(138, 186, 333, 28);
			}
			pack();
			this.setSize(506, 337);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
